/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdListadoClientes.java
 *
 * Created on 05/02/2011, 12:26:09 AM
 */
package com.fsl.sicotec.UI;

import com.fsl.sicotec.Logica.LoCliente;
import com.fsl.sicotec.comun.Entidades.Cliente;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.util.ArrayList;

/**
 *
 * @author User
 */
public class jdListadoClientes extends javax.swing.JDialog {

    //Declarando las variables
    private LoCliente locliente;
    private DefaultTableModel mdlClientes;
    private ArrayList<Cliente> lista;

    /** Creates new form jdListadoClientes */
    public jdListadoClientes(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setSize(790, 560);


        //Instanciando las variables
        locliente = new LoCliente();
        lista = new ArrayList();
        mdlClientes = (DefaultTableModel) jtLista.getModel();
        jtLista.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);

        mostrarClientes();
    }

    public void mostrarClientes() {
        int nregistros = locliente.mostrarClientes().size();
        mdlClientes.setRowCount(nregistros);
        lblnreg.setText("Número de Registros : " + nregistros);
        lista = (ArrayList<Cliente>) locliente.mostrarClientes();
        int i = 0;
        for (Cliente objcliente : lista) {
            mdlClientes.setValueAt(objcliente.getCod_cliente(), i, 0);
            mdlClientes.setValueAt(objcliente.getApe_paterno(), i, 1);
            mdlClientes.setValueAt(objcliente.getApe_materno(), i, 2);
            mdlClientes.setValueAt(objcliente.getNombres(), i, 3);
            mdlClientes.setValueAt(objcliente.getDni(), i, 4);
            mdlClientes.setValueAt(objcliente.getDireccion(), i, 5);
            mdlClientes.setValueAt(objcliente.getTelefono(), i, 6);
            mdlClientes.setValueAt(objcliente.getNombre_contacto(), i, 7);
            i++;
        }
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        btnNuevo = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnImprimir = new javax.swing.JButton();
        btnCerrar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtLista = new javax.swing.JTable();
        lblnreg = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Clientes");

        jToolBar1.setRollover(true);

        btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/fsl/sicotec/UI/Recursos/agregar.png"))); // NOI18N
        btnNuevo.setText("Nuevo");
        btnNuevo.setFocusable(false);
        btnNuevo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnNuevo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });
        jToolBar1.add(btnNuevo);

        btnModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/fsl/sicotec/UI/Recursos/editar.png"))); // NOI18N
        btnModificar.setText("Modificar");
        btnModificar.setFocusable(false);
        btnModificar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnModificar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnModificar);

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/fsl/sicotec/UI/Recursos/eliminar.png"))); // NOI18N
        btnEliminar.setText("Eliminar");
        btnEliminar.setFocusable(false);
        btnEliminar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEliminar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnEliminar);

        btnImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/fsl/sicotec/UI/Recursos/PrintHS.png"))); // NOI18N
        btnImprimir.setText("Imprimir");
        btnImprimir.setFocusable(false);
        btnImprimir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnImprimir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btnImprimir);

        btnCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/fsl/sicotec/UI/Recursos/exit.png"))); // NOI18N
        btnCerrar.setText("Cerrar");
        btnCerrar.setFocusable(false);
        btnCerrar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCerrar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnCerrar);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(" Relación de Clientes "));

        jtLista.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "COD", "AP. PATERNO", "AP. MATERNO", "NOMBRES", "DNI", "DIRECCION", "TELEFONO", "CONTACTO"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jtLista);
        jtLista.getColumnModel().getColumn(0).setResizable(false);
        jtLista.getColumnModel().getColumn(0).setPreferredWidth(40);
        jtLista.getColumnModel().getColumn(1).setResizable(false);
        jtLista.getColumnModel().getColumn(3).setResizable(false);
        jtLista.getColumnModel().getColumn(5).setResizable(false);

        lblnreg.setText("Número de Registros :");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblnreg, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 738, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 372, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lblnreg)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 790, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        setVisible(false);
    }//GEN-LAST:event_btnCerrarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        int fila = jtLista.getSelectedRow();
        int cod_cliente;
        String cliente;
        if (fila != -1) {
            cod_cliente = Integer.parseInt(mdlClientes.getValueAt(fila, 0).toString());
            cliente = mdlClientes.getValueAt(fila, 1).toString() + " "+  mdlClientes.getValueAt(fila, 2).toString() + " , " + mdlClientes.getValueAt(fila, 3).toString();
            int result = javax.swing.JOptionPane.showConfirmDialog(null,"¿Esta seguro de eliminar el cliente : \n" +  cliente  + "?");
            if(result == 0){
                locliente.elimarCliente(cod_cliente, "u0052", "My PC 58");
                javax.swing.JOptionPane.showMessageDialog(null,"Cliente eliminado correctamente");
                mostrarClientes();
            }
        }
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        int fila = jtLista.getSelectedRow();
        int cod_cliente;
        if (fila != -1) {
            cod_cliente = Integer.parseInt(mdlClientes.getValueAt(fila, 0).toString());
            jdCliente jdCli = new jdCliente(this, true, cod_cliente);
            jdCli.setLocationRelativeTo(this);
            jdCli.setVisible(true);
            mostrarClientes();

        }

    }//GEN-LAST:event_btnModificarActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        jdCliente vista = new jdCliente(this, true);
        vista.setLocationRelativeTo(this);
        vista.setVisible(true);
        mostrarClientes();
    }//GEN-LAST:event_btnNuevoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                jdListadoClientes dialog = new jdListadoClientes(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnImprimir;
    private javax.swing.JButton btnModificar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTable jtLista;
    private javax.swing.JLabel lblnreg;
    // End of variables declaration//GEN-END:variables
}
